package com.qf.service.impl;

import com.qf.entity.Browse;
import com.qf.entity.BrowseExample;
import com.qf.mapper.BrowseMapper;
import com.qf.service.UserBrowseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: ZhangYubin
 * @date: 2020/7/25 17:03
 * @description:
 */
@Service
public class UserBrowseServiceImpl implements UserBrowseService {
    @Autowired
    private BrowseMapper browseMapper;

    /**
     * 用户在进入店铺时，增加浏览记录，如果有记录，则更新时间，否则插入新纪录
     * @param userId
     * @param shopId
     * @return
     * @throws ParseException
     */
    @Override
    public boolean addUserBrowse(int userId, int shopId) throws ParseException {
        List<Browse> userBrowseByUserId = getUserBrowseByUserId(userId, shopId);
        System.out.println(userBrowseByUserId==null);
        if (userBrowseByUserId.isEmpty()){
            Browse browse = new Browse();
            browse.setUserId(userId);
            browse.setShopId(shopId);
            Timestamp tiem = new Timestamp(new Date().getTime());//获得系统时间.
            SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
            String nowTime = sdf.format(tiem);
            Date time = sdf.parse(nowTime);
//            Date time= new java.sql.Date(new java.util.Date().getTime());
            browse.setBrowseCreatetime(time);
            int i = browseMapper.insertSelective(browse);
            System.out.println(i);
            if (i != 0){
                return true;
            }else {
                return false;
            }

        }else {
            boolean result = false;
            for (Browse b : userBrowseByUserId) {
                Date date = new Date();//获得系统时间.
                SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
                String nowTime = sdf.format(date);
                Date time = sdf.parse(nowTime);

                b.setBrowseCreatetime(time);
                b.setUserId(userId);
                b.setShopId(shopId);
                int i = browseMapper.updateByPrimaryKeySelective(b);
                if (i != 0){
                    result= true;
                }else {
                    result= false;
                }
            }
            return result;
        }
    }

    /**
     * 检查browse表是否有记录
     * @param userId userid属性
     * @param shopId shopid属性
     * @return 返回Browse的List属性
     */
    @Override
    public List<Browse> getUserBrowseByUserId(int userId,int shopId) {
        BrowseExample example = new BrowseExample();
        BrowseExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andShopIdEqualTo(shopId);
        List<Browse> browses = browseMapper.selectByExample(example);
        System.out.println(browses);
        if (browses != null) {
            return browses;
        }
        return null;
    }
}
